package com.hzj.recipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzj.recipe.entity.Recipe;
import com.hzj.recipe.entity.Step;
import com.hzj.recipe.mapper.RecipeMapper;
import com.hzj.recipe.mapper.StepMapper;
import com.hzj.recipe.resp.RecipeResp;
import com.hzj.recipe.service.RecipeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @author 蜗牛菌
 * @description 针对表【recipe(食谱表)】的数据库操作Service实现
 * @createDate 2024-01-04 10:49:47
 */
@Service
public class RecipeServiceImpl extends ServiceImpl<RecipeMapper, Recipe>
        implements RecipeService {

    @Autowired
    private RecipeMapper recipeMapper;

    @Autowired
    private StepMapper stepMapper;

    @Override
    public Object listAll(Integer index, Integer size, Recipe recipe) {
        //默认第一页开始，每页5条
        if (index == null || index == 0) {
            index = 1;
        }
        if (size == null || size == 0) {
            size = 5;
        }
        Page<Recipe> pageInfo = new Page(index, size);
        LambdaQueryWrapper<Recipe> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (recipe.getRecipeId() != null && recipe.getRecipeId() != 0 && !Objects.equals(recipe.getRecipeId(), "")) {
            lambdaQueryWrapper.eq(Recipe::getRecipeId, recipe.getRecipeId());
        }
        if (recipe.getCategoryId() != null && recipe.getCategoryId() != 0 && !Objects.equals(recipe.getCategoryId(), "")) {
            lambdaQueryWrapper.eq(Recipe::getCategoryId, recipe.getCategoryId());
        }
        if ((recipe.getRecipeName() != null) && !Objects.equals(recipe.getRecipeName(), "")) {
            lambdaQueryWrapper.like(Recipe::getRecipeName, recipe.getRecipeName());
        }
        if ((recipe.getRecipeDescription() != null) && !Objects.equals(recipe.getRecipeDescription(), "")) {
            lambdaQueryWrapper.like(Recipe::getRecipeDescription, recipe.getRecipeDescription());
        }
        if ((recipe.getRecipeDifficulty() != null) && recipe.getRecipeDifficulty() != 0 && !Objects.equals(recipe.getRecipeDifficulty(), "")) {
            lambdaQueryWrapper.like(Recipe::getRecipeDifficulty, recipe.getRecipeDifficulty());
        }
        Page<Recipe> recipePage = recipeMapper.selectPage(pageInfo, lambdaQueryWrapper);

        return recipePage;
    }

    @Override
    public RecipeResp selectByRecipeId(Serializable id) {
        Recipe recipe = recipeMapper.selectById(id);
        //判断是否为空
        if (recipe != null) {
            RecipeResp recipeResp = new RecipeResp();
            recipeResp.setRecipe(recipe);

            //按食谱id找全部步骤
            List<Step> stepList = stepMapper.ListByRecipeId(id);
            if (stepList.size() != 0) {
                recipeResp.setStepList(stepList);
            }
            return recipeResp;
        }
        return null;
    }
}




